<template>
  <div class="rider-rating">
    <el-dialog
      title="评价骑手"
      :visible.sync="dialogVisible"
      width="400px"
      :close-on-click-modal="false"
      :before-close="handleClose">
      <div class="rating-content">
        <div class="rider-info" v-if="rider">
          <img :src="rider.avatar || defaultAvatar" class="rider-avatar">
          <div class="rider-name">{{ rider.realName }}</div>
        </div>
        
        <div class="rating-stars">
          <div class="rating-label">骑手服务评分</div>
          <el-rate
            v-model="rating"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            :texts="['很差', '较差', '一般', '不错', '很好']"
            show-text
            @change="handleRatingChange"
          ></el-rate>
        </div>
        
        <div class="rating-comment">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入评价内容（选填）"
            v-model="comment"
            maxlength="200"
            show-word-limit
          ></el-input>
        </div>
      </div>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitRating" :loading="loading">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { rateRider } from '@/api/order';

export default {
  name: 'RiderRating',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    order: {
      type: Object,
      default: null
    },
    rider: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      defaultAvatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      rating: 5,
      comment: '',
      loading: false
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
      this.reset();
    },
    
    handleRatingChange(value) {
      this.rating = value;
    },
    
    reset() {
      this.rating = 5;
      this.comment = '';
      this.loading = false;
    },
    
    submitRating() {
      if (!this.rating) {
        this.$message.warning('请为骑手的服务进行评分');
        return;
      }
      
      if (!this.order || !this.order.id || !this.rider || !this.rider.id) {
        this.$message.error('订单或骑手信息不完整，无法评价');
        return;
      }
      
      this.loading = true;
      
      // 提交评价
      rateRider({
        orderId: this.order.id,
        riderId: this.rider.id,
        rating: this.rating,
        comment: this.comment
      }).then(response => {
        if (response.code === 200) {
          this.$message.success('评价成功');
          this.handleClose();
          // 触发评价成功事件
          this.$emit('rating-success');
        } else {
          this.$message.error(response.message || '评价失败');
        }
      }).catch(error => {
        console.error('评价骑手异常:', error);
        this.$message.error('评价失败，请稍后重试');
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.rider-rating {
  font-size: 14px;
}

.rating-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
}

.rider-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.rider-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 8px;
}

.rider-name {
  font-size: 16px;
  font-weight: bold;
}

.rating-stars {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.rating-label {
  margin-bottom: 10px;
  font-weight: bold;
}

.rating-comment {
  width: 100%;
  margin-bottom: 10px;
}
</style> 